<template>
  <d2-container>
    <el-form :inline="true">
      <el-button
        type="primary"
        style="float:right;margin:10px;"
        icon="el-icon-plus"
        @click="newCourseType"
      >新建种类</el-button>
    </el-form>
    <el-table :data="categoryList" border style="width: 100%">
      <el-table-column prop="name" label="商品种类"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="editClick(scope.row)" type="text" size="small">修改</el-button>
          <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="10"
      :total="total"
      @current-change="change"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import {mapState} from "vuex";

export default {
  data() {
    return {
      categoryList: [],
      total: 0
    };
  },
    computed:{
    ...mapState('d2admin/user', ['info']),
  },
  methods: {
    newCourseType() {
      this.$prompt("请输入商品种类名称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(({ value }) => {
          request({
            url: "/productCategory/addProductCategory",
            method: "post",
            data: { name: value ,school : this.school}
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "商品种类提交成功"
              });
              this.getCategoryList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    },
    editClick(e) {
      this.$prompt("修改课程种类", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(({ value }) => {
          request({
            url: "/productCategory/updateProductCategory",
            method: "post",
            data: { name: value, id: e._id ,school:this.school}
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "商品种类修改成功"
              });
              this.getCategoryList(this.page);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    },
    deleteClick(e) {
      this.$confirm("此操作将删除该种类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/productCategory/deleteProductCategory",
            method: "post",
            data: { id: e._id }
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "删除成功"
              });
              this.getCategoryList(this.page);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    change(e) {
      this.getCategoryList(e);
    },
    getCategoryList(page) {
      request({
        url: "/productCategory/getProductCategoryList",
        method: "post",
        data: { page: page,pageSize: this.pageSize, school:this.school}
      }).then(res => {
        console.log(res,'hhh')
        this.categoryList = res.data.productCategory;
      });
    },
    // getCategoryList(pageSize) {
    //   request({
    //     url: "/productCategory/getProductCategoryList",
    //     method: "post",
    //     data: { pageSize: pageSize }
    //   }).then(res => {
    //     this.categoryList = res.data.productCategory;
    //   });
    // }
  },
  created() {
    this.school = this.info.school[0]._id
                       this.agent = this.info.staff == undefined ? null : this.info.staff._id
            this.staffname = this.info.staff == undefined ? '' : this.info.staff.name
    this.getCategoryList(1)
  }
};
</script>
